package br.com.skytef.beans.WEB;

import java.io.Serializable;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;

import br.com.skytef.dao.ChartDao;
import br.com.skytef.dao.OrderDao;
import br.com.skytef.entity.TotalPedidosEntity;

@ManagedBean(name = "dashboardBean")
@ViewScoped
public class DashboardBean implements Serializable {
	private CartesianChartModel model1 = new CartesianChartModel();
	private CartesianChartModel model2 = new CartesianChartModel();
	private List<TotalPedidosEntity> dataMaster;



	@PostConstruct
	public void init() throws Exception {
		createLinearModel();
	}
	
	public void loadReportMaster(Date dateIni, Date dateFin) throws SQLException, Exception {
		dataMaster = new OrderDao().buscarTotais(dateIni, dateFin);
	}

	private void createLinearModel() throws SQLException, Exception {
		
		this.loadReportMaster(new Date(), new Date());
				
		model1 = new CartesianChartModel();
		model1.addSeries(getStockChartData("Dado 1"));
		model1.addSeries(getStockChartData("Dado 2"));

		
		int year1 = Calendar.getInstance().get(Calendar.YEAR);
		int year2 = year1 - 1;
		
		model2 = new CartesianChartModel();
		model2.addSeries(getCharMonth("Status " + year2,year2));
		model2.addSeries(getCharMonth("Status " + year1,year1));
	}

	private ChartSeries getCharMonth(String label, int year) {
		ChartDao cDao = new ChartDao();
		Map<String, Double> map = new HashMap<String, Double>();
				
		
		map = cDao.getSalesMonth(year);

		ChartSeries data1 = new ChartSeries();
		data1.setLabel(label);

		
		for (int i = 1; i <= 12; i++) {
			int count = 0;
			String month = this.getMonthShortName(i);
			for (Map.Entry entry : map.entrySet()) {
				
				int key = Integer.parseInt(entry.getKey().toString());								
				if (key == i) {
					count++;
					data1.getData().put(month,(Double) entry.getValue());
				}
			}
			if (count == 0) {
				data1.getData().put(month,0);
			}
		}

		return data1;
	}

	private ChartSeries getStockChartData(String label) {
		ChartSeries data = new ChartSeries();
		data.setLabel(label);
		for (int i = 1; i <= 20; i++) {
			int ran = (int) (Math.random() * 1000);
			data.getData().put(i + "Teste", ran);
		}
		return data;
	}

	public static String getMonthShortName(int monthNumber) {
		String monthName = "";
		monthNumber = monthNumber-1;

		if (monthNumber >= 0 && monthNumber < 12)
			try {
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.MONTH, monthNumber);

				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM");
				simpleDateFormat.setCalendar(calendar);
				monthName = simpleDateFormat.format(calendar.getTime());
			} catch (Exception e) {
				if (e != null)
					e.printStackTrace();
			}
		return monthName;
	}
	
	public void refresh() throws Exception {
		init();
	}
	
	public CartesianChartModel getLinearModel1() {
		return model1;
	}

	public CartesianChartModel getLinearModel2() {
		return model2;
	}

	public List<TotalPedidosEntity> getDataMaster() {
		return this.dataMaster;
	}

}